package vipcard.meichehui.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;
import vipcard.meichehui.domain.*;
import vipcard.meichehui.service.Cardservice;
import vipcard.meichehui.utill.R;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j

public class CardCnotroller {
    @Autowired
    private Cardservice cardservice;

    @PostMapping("/login")//登录
    public R login(@RequestBody User user, HttpSession session) {

        System.out.println(user);
        String login = cardservice.login(user, session);

        System.out.println(login);
        if (login != null) {
            session.setAttribute("username", user.getUsername());
            return R.success("登录成功");
        }

        return R.error("用户名或密码错误");


    }

    @PostMapping("/vipinfo")//开卡前确认会员
    public R vipinfo(@RequestBody Vip vip) {


        System.out.println(vip + "+++++++++++++++++");
        Integer vipinfo = cardservice.vipinfo(vip);

        if (vipinfo == 1) {

            return R.success("验证成功");

        }else if (vipinfo==2){

            return R.error("会员已被绑定");

        }
//查询存在会员是否有绑定用户名
        return R.error("会员不存在");
    }

    @PostMapping("/addcard")//开卡方法
    public Object addcard(@RequestBody Vip vip, HttpSession session) {

        if (session.getAttribute("username") == null) {
            return new RedirectView("index.html");
        }

        Integer addcard = cardservice.addcard(vip, session);


        log.info(String.valueOf(addcard));

        if (addcard == 1) {

            return R.success("添加成功");
        } else if (addcard == 2) {

            return R.error("添加失败请联系管理员");
        }
        return R.error("该用户存在多张卡券请人工计费");

    }

    @GetMapping("/findall")//该账号下所有会员信息
    public R findall(HttpSession session) {

//        if (session.getAttribute("username")==null){
//            System.out.println("未登录");
//            return "/index.html";
//        }
        List<VipVo> findall = cardservice.findall(session);


        return R.success(findall);
    }

    // 历史消费查询
    @GetMapping("/history/{identifier}")
    public R<List<VipVo>> getHistoryRecords(@PathVariable String identifier) {


        return null;


    }

    //核销扣费

    // 核销扣费
    @PostMapping("/deduct")
    public R deductFee(@RequestBody CarMoney carMoney,HttpSession session) {


//        if (carMoney.getHxphone()!=null){
//
//
//        }

        String username = (String) session.getAttribute("username");

        Integer integer = cardservice.deductFee(carMoney,username);

        log.info("成功或失败标记"+integer);
        if (integer ==1) {

            return R.success("扣费完成");
        }else if (integer==6){
            return R.error("当前会员所属用户不是你");
        }else if (integer==3){

            return R.error("会员余额不足无法扣费");
        }


        return R.error("计次卡无法扣费");
    }


    @GetMapping("/selectlogin")
    public R slelctlogin(HttpSession session) {

        if (session.getAttribute("username") != null) {

            return R.success("登录成功");
        }

        return R.error("NOTLOGIN");
    }

    @PostMapping("/selectbyphone")
    public R<List<VipVo>> selectbyphone(@RequestBody VipVo vipVo){




        CardAdd selectbyid = cardservice.selectbyid(vipVo.getTitle());


        System.out.println(selectbyid);
        if (selectbyid!=null){

            ArrayList<VipVo> list = new ArrayList<>();
            VipVo vipVo1 = new VipVo();
            vipVo1.setTitle("卡类型  :"+selectbyid.getRemarks());
            vipVo1.setDescription("余额  :"+selectbyid.getStatus_date());
            System.out.println(vipVo1);
            list.add(vipVo1);
            return R.success(list);
        }
        return R.error("用户不存在");

    }
}
